package com.example.lib_patrol

import android.os.Bundle
import android.view.View
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewbinding.ViewBinding
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.LogUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.example.lib_common.mvi.BaseActivity
import com.example.lib_patrol.add.PointEntity
import com.example.lib_patrol.databinding.ActivityPatrolBinding
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.*
import kotlin.collections.ArrayList

@Route(path = "/patrol/PatrolActivity")
class PatrolActivity : BaseActivity<ActivityPatrolBinding,PatrolViewModel>() {
    val patrolAdapter = PatrolAdapter()
    override fun createVM(): PatrolViewModel {
        return PatrolViewModel()
    }

    override fun createViewBinding(): ViewBinding {
       return ActivityPatrolBinding.inflate(inflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        vm.requestAll()
        v.backBtn.setOnClickListener { finish() }
        bindState()
        v.addPatrol.setOnClickListener {
            ARouter.getInstance().build("/patrol/AddPatrolActivity").navigation()
        }
    }
    var allList : ArrayList<PatrolEntity> = ArrayList()
    private fun bindState() {
        lifecycleScope.launch(Dispatchers.Main){
            repeatOnLifecycle(Lifecycle.State.STARTED){
                vm.mutableContainer.uiStateFlow.collect{
                    if (it.entity.statuesCode==200){
                        allList= it.entity.values as ArrayList<PatrolEntity>
                        bindAdapter()
                    }
                }
            }
        }
    }
    private fun bindAdapter() {
        val  startCalendar= Calendar.getInstance()
        val  endCalendar= Calendar.getInstance()
        val layoutManager = LinearLayoutManager(this)
        layoutManager.orientation= LinearLayoutManager.VERTICAL
        v.myApplyRecycler.layoutManager=layoutManager
        v.myApplyRecycler.adapter=patrolAdapter
        for (patrolEntity in allList) {
            try {
                val pStart = patrolEntity.pStart!!.toLong()
                val pEnd = patrolEntity.pEnd!!.toLong()
                startCalendar.timeInMillis=pStart
                endCalendar.timeInMillis=pEnd
                val startMonth = startCalendar.get(Calendar.MONTH)+1
                val starDay = startCalendar.get(Calendar.DAY_OF_MONTH)
                val endMonth = endCalendar.get(Calendar.MONTH)+1
                val endDay = endCalendar.get(Calendar.DAY_OF_MONTH)
                patrolEntity.mStart=""+startMonth+"月"+starDay+"日"
                patrolEntity.mEnd=""+endMonth+"月"+endDay+"日"
            }catch (e : Exception){

            }
        }
        patrolAdapter.setNewInstance(allList)
        patrolAdapter.setOnItemClickListener { adapter, view, position ->
            val data = adapter.data as ArrayList<PatrolEntity>
//            val type =object : TypeToken<List<PatrolRowEntity>>() {}.type
//            val fromGson=Gson().fromJson(pPoint,type) as ArrayList<PatrolRowEntity>
            ARouter.getInstance().build("/patrol/DetailActivity")
                .withString("pPoint",data[position].pPoint).navigation()
        }
    }
}